﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System;

using ProcessHandles;

public class DataIO
{
    //加载刺激图片的路径
    public static string stimulusBasePath = "F:\\ETDB\\MyDataset\\";

    //参与者文档的路径
    public static string participantTxtPath = "F:\\ETDB\\MyDataset\\participated.txt";

    //完整实验记录的路径
    public static string rawJsonDataBasePath = "F:\\Stimulus\\RawJsonData\\";

    //保留实验记录的路径
    public static string gazepointsDataPath = "F:\\ETDB\\MyDataset\\_GazepointsData\\";

    public static List<string> fileBuffer = new List<string>();

    static public string handlingPath = "";

    private static StreamWriter writer;
    private static StreamReader reader;


    public static List<string> ReadOutTxt(string path = "F:\\ETDB\\MyDataset\\participated.txt")
    {
        fileBuffer.Clear();
        reader = new StreamReader(path, Encoding.UTF8);
        string text;
        while ((text = reader.ReadLine()) != null)
        {
            fileBuffer.Add(text);
        }
        reader.Dispose();
        reader.Close();

        return fileBuffer;
    }

    public static void WriteIntoTxt(string path = "F:\\ETDB\\MyDataset\\participated.txt")
    {
        FileInfo file = new FileInfo(path);
        if (!file.Exists)
        {
            file.CreateText().Dispose();
        }
        File.WriteAllLines(path, fileBuffer);
    }

    public static void WriteSerializedData(bool isChecked = false)
    {
        string path = (isChecked? gazepointsDataPath : rawJsonDataBasePath) +
            (Cfg_Info.s_StimulusGroupIdx.ToString() + "_" + Participant_Info.s_Index + "_" 
            + GenTimeStampOfFileName() + ".json");

        FileInfo file = new FileInfo(path);
        if (!file.Exists)
        {
            file.CreateText().Dispose();
            File.WriteAllLines(path, fileBuffer);

            Debug.Log(path + "\n" + fileBuffer[0]);
        }
        else
        {
            writer = file.AppendText();
        }
    }

    public static void DeleteHandlingFile()
    {
        if(File.Exists(handlingPath))
            File.Delete(handlingPath);
    }

    private static string GenTimeStampOfFileName()
    {
        int hour = DateTime.Now.Hour;
        int minute = DateTime.Now.Minute;
        int second = DateTime.Now.Second;
        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;
        int day = DateTime.Now.Day;

        return string.Format("{3:D4}{4:D2}{5:D2}_" + "{0:D2}{1:D2}{2:D2}"
            , hour, minute, second, year, month, day);
    }


#if false
    /*
    /*-------------------OLD VERSION-------------------*/
    static string basePath =
#if UNITY_EDITOR
        basePath = "D:\\"; //txt文本存储在电脑的某个文件夹
#else
        basePath = Application.dataPath + "/";//txt文本存储在本项目工程路径
#endif
    //把所有的数据写入Txt文本中
    public void WriteIntoTxt(List<Vector3> data)
    {
        _txtPath = basePath + GenFileName() + ".txt";

        FileInfo file = new FileInfo(_txtPath);
        if (!file.Exists)
        {
            writer = file.CreateText();
        }
        else
        {
            writer = file.AppendText();
        }

        foreach(var v3 in data)
        {
            writer.WriteLine(v3.ToString());
        }
        writer.Flush();
        writer.Dispose();
        writer.Close();
    }

    //从Txt读取数据 存储到列表中
    public void ReadOutTxt_()
    {
        fileBuffer.Clear();
        reader = new StreamReader(_txtPath, Encoding.UTF8);
        string text;
        while ((text = reader.ReadLine()) != null)
        {
            fileBuffer.Add(text);
        }
        reader.Dispose();
        reader.Close();
    }


    /// <summary>
    /// 获取从列表读取数据之后的List
    /// </summary>
    /// <returns></returns>
    public List<string> GetmytxtList()
    {
        ReadOutTxt();
        return fileBuffer;
    }

    private string GenFileName()
    {
        int hour = DateTime.Now.Hour;
        int minute = DateTime.Now.Minute;
        int second = DateTime.Now.Second;
        int year = DateTime.Now.Year;
        int month = DateTime.Now.Month;
        int day = DateTime.Now.Day;

        return string.Format("{3:D4}{4:D2}{5:D2}_" + "{0:D2}{1:D2}{2:D2}" 
            , hour, minute, second, year, month, day);
    }
    /*--------------------------------------*/ 

        public static void AddParticipantIntoTxt()
    {
        //if (File.Exists(participantTxtPath))
        //{
        //    string strContent = File.ReadAllText(participantTxtPath);
        //    string[] lines = File.ReadAllText(participantTxtPath).Split(new string[] { "\r\n" }, StringSplitOptions.None);


        //    string line = lines[ProcessHandles.Cfg_Info.s_StimulusGroupIdx - 1];

        //    Debug.Log(line);
        //    Debug.Log(line.Length);
        //    Debug.Log(ProcessHandles.Cfg_Info.s_StimulusGroupIdx);

        //    if (line[0] == 'n')
        //        line = ProcessHandles.Participant_Info.s_Index;
        //    else
        //        line += "," + ProcessHandles.Participant_Info.s_Index;

        //    lines[ProcessHandles.Cfg_Info.s_StimulusGroupIdx - 1] = line;

        //    File.WriteAllLines(participantTxtPath, lines);
        //}

    }
#endif
}